package com.coocaa.aries.jmeter.util;

import com.coocaa.aries.core.base.constant.CharSetConstants;
import com.coocaa.aries.core.base.exception.AriesException;
import com.coocaa.aries.core.base.exception.ExceptionUtil;
import com.coocaa.aries.core.tool.InstanceUtil;
import com.coocaa.aries.core.tool.json.JsonUtil;
import com.coocaa.aries.jmeter.constant.JmeterConfigConstants;
import com.coocaa.aries.jmeter.entity.ExtTestCaseInfoEntity;
import com.coocaa.aries.jmeter.model.JmxTemplateModel;
import com.coocaa.aries.jmeter.yml.JmeterProperties;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * @author : bijiahao
 * @date : 2019/4/18.
 * @description: JMX 文件模板工具
 */
public class JmxTemplateUtil {

    private static final Logger log = LoggerFactory.getLogger(JmxTemplateUtil.class);

    public static void handleTemplate2File(ExtTestCaseInfoEntity entity, JmeterProperties jmeterProperties) throws AriesException {

        //创建一个freemarker.template.Configuration实例，它是存储 FreeMarker 应用级设置的核心部分
        //指定版本号
        Configuration cfg = new Configuration(Configuration.VERSION_2_3_28);
        //获得模板文件路径

        try {
            //设置模板目录
            cfg.setDirectoryForTemplateLoading(new File(FtlTemplateUtil.getTemplateDir(JmeterConfigConstants.TEMPLATE_DIR)));
            //设置默认编码格式
            cfg.setDefaultEncoding(CharSetConstants.UTF8);

            //从设置的目录中获得模板
            Template template = cfg.getTemplate(JmeterConfigConstants.TEMPLATE_FILE_NAME);
            handleTemplate2File(template, entity, jmeterProperties);

        } catch (Exception e) {
            Map errMap = InstanceUtil.newHashMap(2);
            errMap.put("relativeDir", JmeterConfigConstants.TEMPLATE_DIR);
            errMap.put("templateFileName", JmeterConfigConstants.TEMPLATE_FILE_NAME);
            log.error(ExceptionUtil.getErrorPrintLog("获取Jmeter配置模板", errMap, e));
            throw new AriesException("获取Jmeter配置模板异常");
        }
    }

    private static void handleTemplate2File(Template template, ExtTestCaseInfoEntity entity, JmeterProperties jmeterProperties) throws Exception {
        String jmxFilePath;
        if (StringUtils.isBlank(entity.getTestCaseFilePath())) {
            String testCaseFilePath = PathUtil.getRelativeJmxStorePath();
            jmxFilePath = PathUtil.getAbsoluteJmxStorePath(testCaseFilePath, jmeterProperties);
            entity.setTestCaseFilePath(testCaseFilePath);
        } else {
            jmxFilePath = PathUtil.getAbsoluteJmxStorePath(entity.getTestCaseFilePath(), jmeterProperties);
        }
        log.info("\n====jmx存放路径: " + jmxFilePath);
        File file = new File(jmxFilePath);
        if (!file.exists()) {
            FileUtils.forceMkdir(file.getParentFile());
        }
        FileOutputStream out = new FileOutputStream(file);
        OutputStreamWriter writer = new OutputStreamWriter(out, CharSetConstants.UTF8);
        //数据
        Map<String, Object> data = new HashMap<>(1);
        JmxTemplateModel model = (JmxTemplateModel) JsonUtil.json2Object(entity.getJmxConfigJson(), JmxTemplateModel.class);
        data.put("data", model);
        template.process(data, writer);
        out.flush();
        out.close();
    }


}
